package com.iris.live.services.services.impl;

import com.iris.live.services.common.reconsitution.DefaultValueConstant;
import com.iris.live.services.data.models.GroupAreaModel;
import com.iris.live.services.data.models.用户Model;
import com.iris.live.services.data.repositories.GroupAreaRepository;
import com.iris.live.services.data.repositories.用户Repository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import com.iris.live.services.services.BrandService;
import com.iris.live.services.services.GroupAreaService;
import com.iris.live.services.services.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yitianle on 2017/8/11.
 */
@Service
public class GroupAreaServiceImpl implements GroupAreaService {

    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GroupAreaServiceImpl.class);
    @Autowired
    private GroupAreaRepository groupAreaRepository;

    @Resource
    private BrandService brandService;
	
    @Autowired
    private 用户Repository repository用户;

    @Override
    public List<GroupAreaModel> getAreaListByParentId(Integer parentId) {
        return groupAreaRepository.getAreaListByParentIds(parentId,0);
    }

    @Override
    public List<GroupAreaModel> getGroupAreaList(String parentCode) {
        GroupAreaModel parent = groupAreaRepository.getInfoByPermit(parentCode);

        if(parent != null){
            return groupAreaRepository.getAreaListByParentId(parent.getParentId());
        }
        return null;
    }

    @Override
    public Integer getIdByPermit(String permit) {
        return groupAreaRepository.getIdByPermit(permit);
    }

    @Override
    public GroupAreaModel getInfoByPermit(String permit) {
        return  groupAreaRepository.getInfoByPermit(permit);
    }

    @Override
    public GroupAreaModel getParentInfoByParentId(String permit) {
        GroupAreaModel currentModel = getInfoByPermit(permit);
        GroupAreaModel parentModel = null;
        if(null != currentModel){
             parentModel = groupAreaRepository.getParentInfoById(currentModel.getParentId());
        }
        return parentModel;
    }

    @Override
    public List<GroupAreaModel> findByBrandLikeAndPermit(String brand , String permit){
        return  groupAreaRepository.getByBrandLikeAndPermit('%' + brand + '%', permit);
    }


    @Override
    public int createdArea(SystemService.createdAreaBody body) {
        return 0;
    }

    @Override
    public List<GroupAreaModel> getAreaListByBrand(String brand) {
        return groupAreaRepository.getAreaByBrand("%" + brand + "%");
    }
	
    @Override
    public List<GroupAreaModel> getAllBrandAndarea(String userName) {
        List<GroupAreaModel> areaModel = null;
        用户Model userModel = repository用户.findByCode(userName);
        //如果是超级管理员 则返回所有品牌区域，  否则返回 null
        if (userModel!=null && "超级管理员".equals(userModel.get角色())){
            areaModel = groupAreaRepository.getAllBrandAndarea();
        }
        return areaModel;
    }
}
